package com.camel.docker.controller;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
import org.apache.poi.ss.usermodel.charts.DataSources;
import org.apache.poi.ss.usermodel.charts.PieChartData;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

public class PieChartDemo {
	public static void main(String[] args) throws Exception {
		Workbook wb = new XSSFWorkbook();
		Sheet sheet = wb.createSheet("Sheet 1");
        
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("测试");
        
        Row row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("d1");
        row1.createCell(1).setCellValue(3);
        
        Row row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("d2");
        row2.createCell(1).setCellValue(4);
        
        Row row3= sheet.createRow(3);
        row3.createCell(0).setCellValue("d3");
        row3.createCell(1).setCellValue(3);
        
        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 6, 15);
        anchor.setAnchorType(2);
        XSSFChart chart = (XSSFChart)drawing.createChart(anchor);
        PieChartData data = chart.getChartDataFactory().createPieChartData();
        //PieChartData data = new XSSFPieChartData();
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 3, 0, 0));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 3, 1,1));

        data.addSerie(xs, ys1);
        chart.plot(data);
        
        
        FileOutputStream fileOut = new FileOutputStream("F://excel_demo/pie_test.xlsx");
        wb.write(fileOut);
        fileOut.close();
	}
}
